• CONTEXT: A communications equipment manufacturing company has a product which is responsible for emitting informative signals. Company wants to build a machine learning model which can help the company to predict the equipment’s signal quality using various parameters.
• DATA DESCRIPTION: The data set contains information on various signal tests performed:
Parameters: Various measurable signal parameters.
Signal_Quality: Final signal strength or quality
• PROJECT OBJECTIVE: The need is to build a regressor which can use these parameters to determine the signal strength or quality [as number].
from google.colab import drive
drive.mount('/content/drive',force_remount=True)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
import h5py
import tensorflow
import random
random.seed(0)
warnings.filterwarnings('ignore')
%matplotlib inline
tensorflow.__version__
df = pd.read_csv('/content/drive/MyDrive/AIML PROJECT/DEEP LEARNING/Signal.csv')
print(df.shape)
df.head()
The Dataset has 1599 entries and 12 features.
• Perform relevant and detailed statistical analysis on the data.
• Perform relevant and detailed uni, bi and multi variate analysis.
Hint: Use your best analytical approach. Even you can mix match columns to create new columns which can be used for better analysis. Create your own features if required. Be highly experimental and analytical here to find hidden patterns.
# Checking Information
df.info()
The Dataset has no null values and 11 features are float and 1 is integer data types.
# Descriptive statistics
df.describe()
Analysing the 11 parameters : Parameter 1 ranges between 4.6 and 15.9
Maximum value of Parameter 5 is 0.
Parameter 5 has a very low range between 0.047 and 0.611 and also very low standard deviation
Standard deviation is highest for Parameter 7, it is 32.895324
'Signal_Strength' has classes as - Min 3 and Max 8
# Null value check
df.isnull().sum().any()
The Dataset has no null values
# Diplicate value check
df.duplicated().sum()
df[df.duplicated()]
The Dataset has 240 duplicate values but they are not real duplicate values so not droping the values.
# Checking the class of the target
df['Signal_Strength'].value_counts().sort_index()
The are total 6 class of the target variable i.e signal strength.
# Univariate Analysis
fig, axes = plt.subplots(5,2)
fig.set_size_inches(18,16)
a = sns.distplot(df['Parameter 1'], ax=axes[0][0])
a.set_title('Parameter 1 Distribution', fontsize=20)
a = sns.boxplot(df['Parameter 1'], ax = axes[0][1])
a.set_title('Parameter 1 Box Plot', fontsize=20)
a = sns.distplot(df['Parameter 2'], ax=axes[1][0])
a.set_title('Parameter 2 Distribution', fontsize=20)
a = sns.boxplot(df['Parameter 2'], ax = axes[1][1])
a.set_title('Parameter 2 Box Plot', fontsize=20)
a = sns.distplot(df['Parameter 3'], ax=axes[2][0])
a.set_title('Parameter 3 Distribution', fontsize=20)
a = sns.boxplot(df['Parameter 3'], ax = axes[2][1])
a.set_title('Parameter 3 Box Plot', fontsize=20)
a = sns.distplot(df['Parameter 4'], ax=axes[3][0])
a.set_title('Parameter 4 Distribution', fontsize=20)
a = sns.boxplot(df['Parameter 4'], ax = axes[3][1])
a.set_title('Parameter 4 Box Plot', fontsize=20)
a = sns.distplot(df['Parameter 5'], ax=axes[4][0])
a.set_title('Parameter 5 Distribution', fontsize=20)
a = sns.boxplot(df['Parameter 5'], ax = axes[4][1])
a.set_title('Parameter 5 Box Plot', fontsize=20)
plt.tight_layout()
plt.show()
'Parameter 1': Slightly right skewed and some upper outliers
'Parameter 2': Almost normally distributed and some upper outliers
'Parameter 3': Right skewed and one upper outliers
'Parameter 4': Slightly right skewed and some upper outliers
'Parameter 5': Right skewed and some upper outliers
# Univariate Analysis
fig, axes = plt.subplots(6,2)
fig.set_size_inches(18,16)
a = sns.distplot(df['Parameter 6'], ax=axes[0][0])
a.set_title('Parameter 6 Distribution', fontsize=20)
a = sns.boxplot(df['Parameter 6'], ax = axes[0][1])
a.set_title('Parameter 6 Box Plot', fontsize=20)
a = sns.distplot(df['Parameter 7'], ax=axes[1][0])
a.set_title('Parameter 7 Distribution', fontsize=20)
a = sns.boxplot(df['Parameter 7'], ax = axes[1][1])
a.set_title('Parameter 7 Box Plot', fontsize=20)
a = sns.distplot(df['Parameter 8'], ax=axes[2][0])
a.set_title('Parameter 8 Distribution', fontsize=20)
a = sns.boxplot(df['Parameter 8'], ax = axes[2][1])
a.set_title('Parameter 8 Box Plot', fontsize=20)
a = sns.distplot(df['Parameter 9'], ax=axes[3][0])
a.set_title('Parameter 9 Distribution', fontsize=20)
a = sns.boxplot(df['Parameter 9'], ax = axes[3][1])
a.set_title('Parameter 9 Box Plot', fontsize=20)
a = sns.distplot(df['Parameter 10'], ax=axes[4][0])
a.set_title('Parameter 10 Distribution', fontsize=20)
a = sns.boxplot(df['Parameter 10'], ax = axes[4][1])
a.set_title('Parameter 10 Box Plot', fontsize=20)
a = sns.distplot(df['Parameter 11'], ax=axes[5][0])
a.set_title('Parameter 11 Distribution', fontsize=20)
a = sns.boxplot(df['Parameter 11'], ax = axes[5][1])
a.set_title('Parameter 110 Box Plot', fontsize=20)
plt.tight_layout()
plt.show()
'Parameter 6' : Right skewed and some upper outliers
'Parameter 7' : Right skewed and some upper outliers
'Parameter 8' : Normally distributed and some upper and lower outliers
'Parameter 9' : Normally distributed and some upper and lower outliers
'Parameter 10': Right skewed and some upper outliers
'Parameter 11': Right skewed and some upper outliers
# studying the distribution of continuous attributes with mean, median,mode, max & min and std
cols = list(df)
for i in np.arange(len(cols)):
sns.distplot(df[cols[i]], color='blue')
#plt.xlabel('Experience')
plt.show()
print('Distribution of ',cols[i])
print('Mean is:',df[cols[i]].mean())
print('Median is:',df[cols[i]].median())
print('Mode is:',df[cols[i]].mode())
print('Standard deviation is:',df[cols[i]].std())
print('Skewness is:',df[cols[i]].skew())
print('Maximum is:',df[cols[i]].max())
print('Minimum is:',df[cols[i]].min())
# Checking distibution of the column Signal_Strength
sns.countplot(df['Signal_Strength'])
plt.show()
Class 5 has the highest no. of count and then class 6
# Bivariate Analysis
from pylab import rcParams
rcParams['figure.figsize'] = 12,7
sns.scatterplot(df['Parameter 1'], df['Parameter 3'], data=df, hue ='Signal_Strength', palette = 'bright', );
plt.title('Scatter plot between Parameter 1 and Parameter 3', fontsize=15)
Thre is a linear relationship between these two parameter. It indicates the sensor is giving contineous of 5, 6 and 7 at this readings
# Bivariate Analysis
sns.scatterplot(df['Parameter 1'], df['Parameter 2'], data=df, hue ='Signal_Strength', palette = 'bright', );
plt.title('Scatter plot between Parameter 1 and Parameter 2', fontsize=15)
Non linear relationship between these two features. Data piont are spreaded like a cloud
# Bivariate Analysis
sns.scatterplot(df['Parameter 1'], df['Parameter 8'], data=df, hue ='Signal_Strength', palette = 'bright', );
plt.title('Scatter plot between Parameter 1 and Parameter 8', fontsize=15)
Thre is a strong linear relationship between these two parameter. Indiactes that there is a multicolinearity in the dataset
sns.scatterplot(df['Parameter 4'], df['Signal_Strength'], data=df);
plt.title('Scatter plot between Parameter 4 and Singnal Strenght', fontsize=15)
Signal strenght is good between 2 - 7
# Bivariate Analysis
sns.scatterplot(df['Parameter 10'], df['Signal_Strength'], data=df);
plt.title('Scatter plot between Parameter 10 and Singnal Strenght', fontsize=15)
Signal strenght is good between 0.3 - 1.0 reading. This feature is not a good predictor for signal strenght
# Bivariate Analysis
sns.scatterplot(df['Parameter 9'], df['Signal_Strength'], data=df);
plt.title('Scatter plot between Parameter 9 and Singnal Strenght', fontsize=15)
The sensor is giving a continues strenght between the range of 3 - 3.6
#Multivariate Analysis
sns.pairplot(df, diag_kind='kde');
Some features are distributed like cloud
Parameter 1 is +ve linearly corelated with Parameter 3 and Parameter 8
-ve linear coralation between Parameter 1 and 9
The digonal image of pairplot indicates that some features are normally distributed and some are right skewed
# Outliers Check
df.iloc[:,:11].plot(kind='box', subplots=True, figsize=(20,8));
plt.tight_layout()
All the features have upper and lower outliers
# Treating the outliers using IQR methods
cols = (list(df.iloc[:,:11]))
def out_limit(col):
Q1, Q3 = col.quantile([0.25, 0.75])
IQR = Q3-Q1
lower_range = Q1 - (1.5*IQR)
upper_range = Q3 + (1.5*IQR)
return lower_range, upper_range
for col in cols:
lr, ur = out_limit(df[col])
df[col] = np.where(df[col] < lr, lr, df[col])
df[col] = np.where(df[col] > ur, ur, df[col])
# Rechecking Outliers
df.iloc[:,:11].plot(kind='box', subplots=True, figsize=(20,8));
plt.tight_layout()
All the outliers have been removed using IQR methods.
# Coleration Heatmap
plt.figure(figsize=(16,10))
sns.heatmap(df.corr(), annot=True, vmax=1, vmin=-1,cmap='plasma',mask=np.triu(df.corr(),+1));
Heatmap says features are highly +ve(Box colored with light yellow and orange color) and -ve(Box colored with light and dark blue color) corelated with eachother
Parameter 11 shows some good corelation for predictting the strength
# Seperating the indipendent and target variable
X = df.drop('Signal_Strength', axis=1)
y = df.pop('Signal_Strength')
X.head()
# Spliting the data into Training and Testing
from sklearn.model_selection import train_test_split
X_vtrain, X_test, y_vtrain, y_test = train_test_split(X, y, test_size=0.30, random_state=1)
print(X_vtrain.shape)
print(X_test.shape)
print(y_vtrain.shape)
print(y_test.shape)
# Now creating the training and validation data
X_train, X_val, y_train, y_val = train_test_split(X_vtrain, y_vtrain, test_size=0.20, random_state=1)
# Building Neurral Network
from keras.models import Sequential
from keras.wrappers.scikit_learn import KerasClassifier
from keras.layers import Dense, Dropout, BatchNormalization
from sklearn.model_selection import GridSearchCV
from tensorflow.keras import regularizers, optimizers
# Initialize Sequential model
model_reg = tensorflow.keras.models.Sequential()
# Normalizing the input Data
model_reg.add(tensorflow.keras.layers.BatchNormalization(input_shape= (11,)))
# Dense layer for prediction
model_reg.add(tensorflow.keras.layers.Dense(1))
# Compiling the model
model_reg.compile(optimizer='sgd', loss = 'mse')
# Fitting the training data to the model
model_reg.fit(X_train, y_train, epochs=100, validation_data=(X_val, y_val), batch_size=10)
# save the model
from tensorflow.keras.models import load_model
model_reg.save("model_reg.h5") #using h5 extension
print("model saved!!!")
# load the model
from tensorflow.keras.models import load_model
model_reg = load_model('model_reg.h5')
# Prediction on testing data
ytest_predict = model_reg.predict(X_test)
print(ytest_predict[0])
print(ytest_predict[1])
print(ytest_predict[2])
print(ytest_predict[3])
print(ytest_predict[4])
print(ytest_predict[5])
print(y_test.head())
ytrain_predict = model_reg.predict(X_train)
print(ytrain_predict[0])
print(ytrain_predict[1])
print(ytrain_predict[2])
print(ytrain_predict[3])
print(ytrain_predict[4])
print(ytrain_predict[5])
y_train.head()
# Training data score
from sklearn.metrics import r2_score
r2_score(y_train, ytrain_predict)
Prediction are very similler to each other
# Testing data score
from sklearn.metrics import r2_score
r2_score(y_test, ytest_predict)
The regression model is giving 35% score on test data.
print(model_reg.summary())
# save the model
model_reg.save("model_reg.h5") #using h5 extension
print("model saved!!!")
Model is pickled and saved.
ds = pd.read_csv('/content/drive/MyDrive/AIML PROJECT/DEEP LEARNING/Signal.csv')
print(ds.shape)
ds.head()
• Perform relevant and detailed statistical analysis on the data.
• Perform relevant and detailed uni, bi and multi variate analysis.
Hint: Use your best analytical approach. Even you can mix match columns to create new columns which can be used for better analysis. Create your own features if required. Be highly experimental and analytical here to find hidden patterns.
ds.describe()
Analysing the 11 parameters : Parameter 1 ranges between 4.6 and 15.9
Maximum value of Parameter 5 is 0.
Parameter 5 has a very low range between 0.047 and 0.611 and also very low standard deviation
Standard deviation is highest for Parameter 7, it is 32.895324
'Signal_Strength' has classes as - Min 3 and Max 8
ds.info()
No null value and features are float and integer
ds.isnull().sum().any()
No null values
ds.duplicated().sum()
Here I am not droping the duplicates as they are not real duplicates
# Univariate Analysis
cols = list(ds)
for i in np.arange(len(cols)):
sns.distplot(ds[cols[i]], color='green')
plt.show()
print('Distributions of ', cols[i])
print('Mean is:', ds[cols[i]].mean())
print('Median is:', ds[cols[i]].median())
print('Mode is:', ds[cols[i]].mode())
print('Standard deviation is:', ds[cols[i]].std())
print('Skewness is:',ds[cols[i]].skew())
print('Maximum is:',ds[cols[i]].max())
print('Minimum is:',ds[cols[i]].min())
Some features are normally distributed and some are right skewed.
# Bivariate Analysis
from pylab import rcParams
rcParams['figure.figsize'] = 12,7
sns.scatterplot(ds['Parameter 1'], ds['Parameter 3'], data=ds, hue ='Signal_Strength', palette = 'bright', );
plt.title('Scatter plot between Parameter 1 and Parameter 3', fontsize=15)
There is a linear relation between Parameter 1 and Parameter 2
# Multivariate Analysis
sns.pairplot(ds, diag_kind='kde');
Some features are corelated with each other and some are distributed like cloud.
# Coleration Heatmap
plt.figure(figsize=(16,10))
sns.heatmap(ds.corr(), annot=True, vmax=1, vmin=-1,cmap='plasma',mask=np.triu(ds.corr(),+1));
Features are +vely and -vely corelated with eachother
# Outliers Check
ds.iloc[:,:11].plot(kind='box', subplots=True, figsize=(20,8));
plt.tight_layout()
# Treating the outliers using IQR methods
cols = (list(ds.iloc[:,:11]))
def out_limit(col):
Q1, Q3 = col.quantile([0.25, 0.75])
IQR = Q3-Q1
lower_range = Q1 - (1.5*IQR)
upper_range = Q3 + (1.5*IQR)
return lower_range, upper_range
for col in cols:
lr, ur = out_limit(ds[col])
ds[col] = np.where(ds[col] < lr, lr, ds[col])
ds[col] = np.where(ds[col] > ur, ur, ds[col])
# Rechecking Outliers
ds.iloc[:,:11].plot(kind='box', subplots=True, figsize=(20,8));
plt.tight_layout()
Now the dataset has no outliers
# Seperating the indipendent and target variable
X = ds.drop('Signal_Strength', axis=1)
y = ds.pop('Signal_Strength')
print(X.shape)
X.head()
y.nunique()
There are total 6 classes in the target variable
from keras.utils.np_utils import to_categorical
y= to_categorical(y)
y[0]
# splitting data for test for classification
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.30, random_state=1)
print("Shape of y_train:", y_train.shape)
print("One value of y_train:", y_train[0])
# splitting data for train and validation of classification
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=.20, random_state=1)
print("Shape of y_train:", y_train.shape)
print("One value of y_train:", y_train[0])
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
from keras.layers import Dense, Dropout, BatchNormalization
from keras.callbacks import EarlyStopping
# Building the ANN
model_class = Sequential()
model_class.add(Dense(units=255, activation='relu', input_shape=(11,)))
model_class.add(BatchNormalization())
model_class.add(Dense(units=100, activation='relu'))
model_class.add(Dense(units=50, activation='relu'))
model_class.add(Dense(units=9, activation='softmax'))
adam = optimizers.Adam(learning_rate=0.001)
# Compiling the model
model_class.compile(loss='categorical_crossentropy', metrics=['accuracy'],optimizer= adam)
# Model summary
model_class.summary()
# Fit the model
history = model_class.fit(X_train, y_train, batch_size=15, epochs=100, validation_data=(X_val, y_val))
# save the model
model_class.save("model_class.h5") #using h5 extension
print("model saved!!!")
# load the model
model_class = load_model('model_class.h5')
# Summary history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper right')
plt.show()
Initially the training and validation loss was high and started decreasing as traing continue but the training loss is contineously dereasing but the validation loss is flat due to very less no of data.
# Summary history for loss
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper right')
plt.show()
The training accuracy is increasing contineously but the validation score is flat throught the 100 epochs.
score_train = model_class.evaluate(X_train, y_train, verbose=0)
print(model_class.metrics_names)
print(score_train)
The accuracy on the training data is 76% and loss is 0.568.
score_test = model_class.evaluate(X_test, y_test, verbose=0)
print(model_class.metrics_names)
print(score_test)
The accuracy on the testing data is 59% and loss is 1.182.
Y_pred_cls = model_class.predict_classes(X_test)
print(y_train[0],y_train[1],y_train[2],y_train[3])
print(Y_pred_cls[0],Y_pred_cls[1],Y_pred_cls[2],Y_pred_cls[3])
Final Summary:
We can see that regression model gives an R2 of 0.35 where as the classification model give an accuracy of 59% on the test data, The classificatiom model uses more parameter when compared to regression
• BUSINESS CONTEXT: A Recognising multi-digit numbers in photographs captured at street level is an important component of modern-day map making. A classic example of a corpus of such street-level photographs is Google’s Street View imagery composed of hundreds of millions of geo-located 360-degree panoramic images.
• PROJECT OBJECTIVE: We will build a digit classifier on the SVHN (Street View Housing Number) dataset
#Open the file as readonly
import h5py
df = h5py.File('/content/drive/MyDrive/AIML PROJECT/DEEP LEARNING/CNN/Part - 4 - Autonomous_Vehicles_SVHN_single_grey1.h5')
df.keys()
df.values()
# Checking the shape
print(df['X_train'])
print(df['y_train'])
print(df['X_test'])
print(df['y_test'])
print(df['X_val'])
print(df['y_val'])
Training data has 42000 images with the size of 32x32
Validation data has 60000 images with the size of 32x32
Testing data has 18000 images with the size of 32x32
# Converting the data into proper formate
X_train = df['X_train']
X_test = df['X_test']
X_val = df['X_val']
y_train = df['y_train']
y_test = df['y_test']
y_val = df['y_val']
The data is converted into proper formate
# Vizualizing some images on training data using matplotlib
import matplotlib.pyplot as plt
print('Label:', y_train[1000])
plt.imshow(X_train[1000], cmap='gray');
# Visualizing images on test data
print('Label:', y_test[1000])
plt.imshow(X_test[1000], cmap='gray');
Here the image is not clear. lets see how my model is going to predict.
# # visualizing the 25 images in the test dataset
%matplotlib inline
import matplotlib.pyplot as plt
columns = 10
rows = 10
fig=plt.figure(figsize=(8, 8))
for i in range(1, columns*rows + 1):
img = X_test[i]
fig.add_subplot(rows, columns, i)
print (y_test[i], end=' ')
if i % columns == 0:
print ("")
plt.imshow(img, cmap='gray')
plt.show()
X_train = np.asarray(X_train).reshape(42000, 1024) # From 2D to 1D 32x32 = 1024
X_test = np.asarray(X_test).reshape(18000, 1024)
X_val = np.asarray(X_val).reshape(60000, 1024)
print(y_train[0])
y_train = tensorflow.keras.utils.to_categorical(y_train, num_classes=10)
y_val = tensorflow.keras.utils.to_categorical(y_val, num_classes=10)
y_test = tensorflow.keras.utils.to_categorical(y_test, num_classes=10)
print(y_train[0])
# Checking the final shape
print('The shape of the training dataset:', X_train.shape)
print('The shape of the training (y) dataset:', y_train.shape)
print('The shape of the validation dataset:', X_val.shape)
print('The shape of the validation (y) dataset:', y_val.shape)
print('The shape of the test dataset:', X_test.shape)
print('The shape of the test (y) dataset:', y_test.shape)
Now the the data is normalized and tragets in the the binary class matrix
Hint: Use best approach to refine and tune the data or the model. Be highly experimental here to get the best accuracy out of the model
from keras.callbacks import EarlyStopping
# Creating the model
def train_test_loop(iterations, lr, Lambda, verb=True):
# Hyperparameters
iterations = iterations
learning_rate = lr
hidden_nodes = 256
output_nodes = 10
model = Sequential()
model.add(tensorflow.keras.layers.BatchNormalization(input_shape=(1024,)))
model.add(Dense(hidden_nodes, activation='relu', input_shape= (1024,)))
model.add(Dense(hidden_nodes, activation='relu'))
model.add(Dense(output_nodes, activation='softmax', kernel_regularizer=regularizers.l2(Lambda)))
sgd = optimizers.SGD(learning_rate=lr, momentum=0.9, decay = 1e-6)
# Model compile
model.compile(loss='categorical_crossentropy', optimizer= sgd, metrics=['accuracy'])
# Use earlystopping
callback = tensorflow.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=2, min_delta=0.01)
# Fitting the model
model.fit(X_train, y_train, epochs = iterations, batch_size = 200,
validation_data=(X_val, y_val), verbose=1, callbacks=[callback])
score = model.evaluate(X_val, y_val, verbose=0)
print(score)
return score
import math
for k in range(1,5):
lr = math.pow(10, np.random.uniform(-4.0, -0.1))
Lambda = math.pow(10, np.random.uniform(-4,-2))
best_acc = train_test_loop(100, lr, Lambda, False)
print("Try {0}/{1}: Best_val_acc: {2}, lr: {3}, Lambda: {4}\n".format(k, 100, best_acc, lr, Lambda))
After fine search picking learning rate and lambda as Lambda = 1e-4, lr = 2e-2 for final run.
# After fine search doing final iteration on picked learning rate and lambda
# Hyperparameters
Lambda = 1e-4
lr = 2e-2
hidden_nodes = 256
output_nodes = 10
#Initialize the Artificial Neural Network Classifier
model = Sequential()
model.add(tensorflow.keras.layers.BatchNormalization(input_shape=(1024,)))
model.add(Dense(hidden_nodes, activation='relu', input_shape= (1024,)))
model.add(Dense(hidden_nodes, activation='relu'))
model.add(Dense(output_nodes, activation='softmax', kernel_regularizer=regularizers.l2(Lambda)))
sgd = optimizers.SGD(learning_rate=lr, momentum=0.9, decay = 1e-6)
# Model compile
model.compile(loss='categorical_crossentropy', optimizer= sgd, metrics=['accuracy'])
# Use earlystopping
callback = tensorflow.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=2, min_delta=0.01)
# Fitting the model
history = model.fit(X_train, y_train, epochs = 100, batch_size = 200,
validation_data=(X_val, y_val), verbose=1, callbacks=[callback])
Now, after final iteration the model has performed well on training and validation data with the accuracy of 88% and 87% respectivily
Loss on training was 0.03704 and on validation 0.4430
# Evaluating validation data
validation_score = model.evaluate(X_val, y_val)
print('Validation accuracy using SGD:', validation_score[1])
import keras
from matplotlib import pyplot as plt
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['train', 'val'], loc='upper right')
plt.show()
Concl:
After fine tuning of hyperparamter (lanbda, learning_rate) my trainign loss is contineously decreasing using 100 epochs but the model has stoped early using callbacks function at 11 epochs only.
The validation loss is reasonable with 0.4430 and accuracy 87%.
We can further reduce the loss after more tuning.
# Ploting the training loss, validation loss vs number of epochs
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
Concl:
The graph shows both the training and testing doing extreamly well.
Validation data has also giving a very good accuracy of 87%.
Now will check performance on test data for final evaluation.
# Fitting the model on test data to find the accuracy
test_score = model.evaluate(X_test, y_test)
print('Test accuracy using SGD:', test_score[1])
The accuracy and loss on test data is 0.643 and 83% respectivily.
# Image from test data
plt.imshow(X_test[2].reshape(32,32), cmap='gray');
# Predicting the digits on test data
print('Label:', model.predict_classes(X_test)[2])
We can see that the model is working well on the test data. Image of 2 is predicted as 2.bit_length
Lets check another image.
# Image from test data
plt.imshow(X_test[5].reshape(32,32), cmap='gray');
# Predicting the digits on test data
print('Label:', model.predict_classes(X_test)[5])
Predicting image of 9 as 9.
# Image from test data
plt.imshow(X_test[0].reshape(32,32), cmap='gray');
# Predicting the digits on test data
print('Label:', model.predict_classes(X_test)[0])
With more no. of digits the model is not able to predict. The model is predicting this image as 0(zero)
# Predicting on the rest data
ytest_predict = model.predict_classes(X_test)
print(ytest_predict)
#visualising the first 25 images of the test data and printing their labels
print('Actual labels corresponding to below images: %s' % (df['y_test'][0:25]))
plt.figure(figsize=(25,1))
for i in range(25):
plt.subplot(1, 25, i+1)
plt.imshow(X_test[i].reshape(32,32), cmap='gray')
plt.axis('off')
plt.show()
print('Predicted labels corresponding to above images: %s' % (ytest_predict[0:25]))
As we can see above the model appers to predict images with single digits/double digits very well
But not able to predict on three digit images.